﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Infragistics.WebUI.UltraWebGrid;

public partial class page_OperatorInCompetence : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            LoadUsers();                        //加载所有有权限的会员
            LoadGroup();                        //加载所有的组
            LoadCompetences();                  //加载所有的权限 

            LoadUserInGroup(_Customer.ID);      //加载某操作员所拥有的组信息
            LoadUserInCompetence(_Customer.ID); //加载某操作员所拥有权限 
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = Request.Url.AbsoluteUri;

        RequestCompetences = Competences.BuildCompetencesList(Competences.Administrator);

        base.OnInit(e);
    }

    #endregion

    public void LoadUsers()
    {
        DataTable dt = DAL.Functions.F_GetManagers(_Site.ID);

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        lbUser.DataSource = dt;
        lbUser.DataTextField = "CustomerNo";
        lbUser.DataValueField = "ID";

        lbUser.DataBind();

        if (lbUser.Items.Count > 0)
        {
            lbUser.SelectedIndex = 0;
            lbUser_SelectedIndexChanged(lbUser, new EventArgs());
        }
    }

    public void LoadGroup()
    {
        g.DataSource = new DAL.Tables.tbl_CustomerGroups().Open("", "", "[ID]");
        g.DataBind();
    }

    public void LoadCompetences()
    {
        g1.DataSource = new DAL.Tables.tbl_Competences().Open("", "", "[ID]");
        g1.DataBind();
    }

    /// <summary>
    /// 加载会员所拥有的组
    /// </summary>  
    public void LoadUserInGroup(long UserID)
    {
        DataTable dt = null;

        dt = new DAL.Tables.tbl_CustomerInGroups().Open("[GroupID]", "[UserID]=" + UserID, "");

        if (dt != null)
        {
            for (int i = 0; i < g.Items.Count; i++)
            {
                CheckBox chkBox = (CheckBox)g.Items[i].Cells[0].FindControl("chkGroup");

                chkBox.Checked = false;      //将已前的状态清除
            }

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                string tempID = dt.Rows[j][0].ToString(); //查询出该组所有权限

                for (int i = 0; i < g.Items.Count; i++)
                {
                    string id = g.Items[i].Cells[3].Text;

                    if (tempID == id)
                    {
                        CheckBox chkBox = (CheckBox)g.Items[i].Cells[0].FindControl("chkGroup");

                        chkBox.Checked = true;      //将状态置为选中                        
                    }
                }
            }
        }
    }

    /// <summary>
    /// 加载会员所拥有的权限
    /// </summary>    
    public void LoadUserInCompetence(long UserID)
    {
        DataSet ds = null;
        DataTable dt = null;

        DAL.Procedures.P_GetCustomerCompetences(ref ds, UserID);

        try
        {
            dt = ds.Tables[0];
        }
        catch { }

        if (dt != null)
        {
            for (int i = 0; i < g1.Items.Count; i++)
            {
                CheckBox chkBox = (CheckBox)g1.Items[i].Cells[0].FindControl("chkCompetence");

                chkBox.Checked = false;      //将已前的状态清除
            }

            for (int j = 0; j < dt.Rows.Count; j++)
            {
                string tempID = dt.Rows[j][0].ToString(); //查询出该组所有权限

                for (int i = 0; i < g1.Items.Count; i++)
                {
                    string id = g1.Items[i].Cells[3].Text;

                    if (tempID == id)
                    {
                        CheckBox chkBox = (CheckBox)g1.Items[i].Cells[0].FindControl("chkCompetence");

                        chkBox.Checked = true;      //将状态置为选中                        
                    }
                }
            }
        }
    }

    /// <summary>
    /// 新增会员
    /// </summary>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (tbUserName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "用户名不能为空！");

            return;
        }

        if (lbUser.Items.FindByText(tbUserName.Text.Trim()) != null)
        {
            Shove._Web.JavaScript.Alert(this.Page, "会员已经存在于列表中！");

            return;
        }

        Customer tu = new Customer(_Site.ID);
        tu.CustomerNO = tbUserName.Text.Trim();

        string Return = "";
        int i = tu.GetCustomerInformationByNO(ref Return);

        if (i < 0)
        {
            tbUserName.Text = "";
            Shove._Web.JavaScript.Alert(this.Page, "用户名不存在！");

            return;
        }

        lbUser.Items.Add(new ListItem(tu.CustomerNO, tu.ID.ToString()));

        lbUser.SelectedIndex = lbUser.Items.Count - 1;
        lbUser_SelectedIndexChanged(lbUser, new EventArgs());
    }

    /// <summary>
    /// 更新权限
    /// </summary>
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        long UserID = 0;
        string sCompetenceID = "";
        string sGroupID = "";
        string ReturnDescription = "";

        UserID = Shove._Convert.StrToLong(lbUser.SelectedValue, 0);

        if (UserID == 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "请先选择会员！");
            return;
        }

        for (int i = 0; i < g.Items.Count; i++)
        {
            CheckBox chkBox = (CheckBox)g.Items[i].Cells[0].FindControl("chkGroup");

            if (chkBox.Checked)//该行否已选
            {
                sGroupID += "[" + (g.Items[i].Cells[3].Text) + "]";
            }
        }

        for (int i = 0; i < g1.Items.Count; i++)
        {
            CheckBox chkBox = (CheckBox)g1.Items[i].Cells[0].FindControl("chkCompetence");

            if (chkBox.Checked)//该行否已选
            {
                sCompetenceID += "[" + (g1.Items[i].Cells[3].Text) + "]";
            }
        }

        if (DAL.Procedures.P_SetCustomerCompetences(UserID, sCompetenceID, sGroupID, _Site.ID, ref ReturnDescription) >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "更新成功！");
        }
    }

    protected void lbUser_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (lbUser.Items.Count < 1)
        {
            return;
        }

        LoadUserInGroup(Shove._Convert.StrToLong(lbUser.SelectedValue, 0));
        LoadUserInCompetence(Shove._Convert.StrToLong(lbUser.SelectedValue, 0));

        // 超级管理员的权限不能修改。
        if (_Customer.ID == long.Parse(lbUser.SelectedValue) && _Customer.ID == 1)
        {
            g.Enabled = false;
            g1.Enabled = false;
        }
        else
        {
            g.Enabled = true;
            g1.Enabled = true;
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        LoadCompetences();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        LoadCompetences();
    }

}
